package com.hankol.feature_wine_meal.adapter

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import androidx.viewpager.widget.PagerAdapter
import com.baselibrary.utils.GlideUtils
import com.hankol.feature_wine_meal.R

/**
 * @author: 关忆北、
 * @date: 2022/7/4
 * @Des：
 */
class FinishedProductAdapter : PagerAdapter() {
    private var listener: OnImgClickListener? = null
    private var imgs: ArrayList<String> = arrayListOf()
    fun setData(list: ArrayList<String>) {
        imgs.clear()
        imgs.addAll(list)
        imgs.add(list[0])
        imgs.add(0, list[list.size - 1])
        notifyDataSetChanged()
    }

    fun setOnClickListener(listener: OnImgClickListener) {
        this.listener = listener
    }

    override fun getCount(): Int {
        return imgs.size
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val inflate = LayoutInflater.from(container.context).inflate(R.layout.item_wine_details_finished, container, false)
        GlideUtils.loadImg_228(container.context.applicationContext, imgs[position], inflate.findViewById(R.id.iv_wine_details_finished))
        inflate.setOnClickListener {
            listener?.onClick(imgs, position)
        }
        container.addView(inflate)
        return inflate
    }

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view == `object`
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        container.removeView(`object` as View)
    }

    interface OnImgClickListener {
        fun onClick(list: List<String>, position: Int)
    }
}